Q1¶

In [ ]:
import helper
import cv2
In [ ]:
# Loading Image
from skimage.io import imread,imshow,imsave
import numpy as np
from matplotlib import pyplot as plt
img = imread("barbara_gray.bmp")
In [ ]:
def PSNR(img1, img2):
    MSE = np.sum((img1 - img2) ** 2) / (img1.shape[0] * img1.shape[1])
    if MSE == 0:
        return 100
    PIXEL_MAXVALUE = 255.0
    return 10 * np.log10(PIXEL_MAXVALUE**2 / MSE)
In [ ]:
plt.figure()
plt.subplots(figsize=(15, 5))
plt.subplot(1,3,1)
plt.imshow(img,cmap=plt.get_cmap('gray'))
plt.title("original")
plt.show()
<Figure size 640x480 with 0 Axes>
In [ ]:
# 5% corrupted image
noise_image_5  = helper.salt_pepper_noise(img,0.05)
In [ ]:
removed_noise_5 = helper.median_filter(noise_image_5,5)
In [ ]:
plt.figure()
plt.subplots(figsize=(15, 5))
plt.subplot(1,3,1)
plt.imshow(img,cmap=plt.get_cmap('gray'))
plt.title("original")

plt.subplot(1,3,2)
plt.imshow(noise_image_5,cmap=plt.get_cmap('gray'))
plt.title("noise_image_5")

plt.subplot(1,3,3)
plt.imshow(removed_noise_5,cmap=plt.get_cmap('gray'))
plt.title("removed_noise_5")

plt.tight_layout()
plt.show()
<Figure size 640x480 with 0 Axes>
In [ ]:
#   15% corrupted image
noise_image_15  = helper.salt_pepper_noise(img,0.15)

# Remove noise from the image
removed_noise_15 = helper.median_filter(noise_image_15,5)
In [ ]:
# plot the images
plt.figure()
plt.subplots(figsize=(15, 5))
plt.subplot(1,3,1)
plt.imshow(img,cmap=plt.get_cmap('gray'))
plt.title("original")

plt.subplot(1,3,2)
plt.imshow(noise_image_15,cmap=plt.get_cmap('gray'))
plt.title("noise_image_15")

plt.subplot(1,3,3)
plt.imshow(removed_noise_15,cmap=plt.get_cmap('gray'))
plt.title("removed_noise_15")

plt.tight_layout()
plt.show()
<Figure size 640x480 with 0 Axes>
In [ ]:
#  20% corrupted image
noise_image_20  = helper.salt_pepper_noise(img,0.20)

# Remove noise from the image
removed_noise_20 = helper.median_filter(noise_image_20,7)
In [ ]:
# plot the images
plt.figure()
plt.subplots(figsize=(15, 5))
plt.subplot(1,3,1)
plt.imshow(img,cmap=plt.get_cmap('gray'))
plt.title("original")

plt.subplot(1,3,2)
plt.imshow(noise_image_20,cmap=plt.get_cmap('gray'))
plt.title("noise_image_20")

plt.subplot(1,3,3)
plt.imshow(removed_noise_20,cmap=plt.get_cmap('gray'))
plt.title("removed_noise_20")

plt.tight_layout()
plt.show()
<Figure size 640x480 with 0 Axes>
In [ ]:
#  25% corrupted image
noise_image_25  = helper.salt_pepper_noise(img,0.25)
# Remove noise from the image
removed_noise_25 = helper.median_filter(noise_image_25,9)

removed_noise_257 = helper.median_filter(noise_image_25,7)

best is kernal with size 7*7¶

In [ ]:
# plot the images
plt.figure()
plt.subplots(figsize=(15, 5))
plt.subplot(1,4,1)
plt.imshow(img,cmap=plt.get_cmap('gray'))
plt.title("original")

plt.subplot(1,4,2)
plt.imshow(noise_image_25,cmap=plt.get_cmap('gray'))
plt.title("noise_image_25")

plt.subplot(1,4,3)
plt.imshow(removed_noise_25,cmap=plt.get_cmap('gray'))
plt.title("removed_noise_25")

plt.subplot(1,4,4)
plt.imshow(removed_noise_257,cmap=plt.get_cmap('gray'))
plt.title("removed_noise_25_7")

plt.tight_layout()
plt.show()
<Figure size 640x480 with 0 Axes>
In [ ]:
#Psnr
print("PSNR for 5% noise image is ",cv2.PSNR(noise_image_5,removed_noise_5))
print("PSNR for 15% noise image is ",cv2.PSNR(noise_image_15,removed_noise_15))
print("PSNR for 20% noise image is ",cv2.PSNR(noise_image_20,removed_noise_20))
print("PSNR for 25% noise image is ",cv2.PSNR(noise_image_25,removed_noise_25))
print("PSNR for 25% with kernal size 7*7 noise image is ",cv2.PSNR(noise_image_25,removed_noise_257))
PSNR for 5% noise image is  14.009187864798987
PSNR for 15% noise image is  9.959462219440645
PSNR for 20% noise image is  8.84052296004791
PSNR for 25% noise image is  7.948118565221031
PSNR for 25% with kernal size 7*7 noise image is  7.921379270139154

Q2¶

In [ ]:
import helper
import cv2
# Loading Image
from skimage.io import imread,imshow,imsave
import numpy as np
from matplotlib import pyplot as plt
In [ ]:
# Chnaging 256*256 image to 64*64
img = cv2.imread('cameraman.png',0)
output_64 = helper.reduce_image_size(img.copy())
    
In [ ]:
nn_interpolation = helper.nearest_neighbour_interpolation(output_64,4)
(128, 128)
In [ ]:
# plot the images
plt.figure()
plt.subplots(figsize=(15, 5))
plt.subplot(1,3,1)
plt.imshow(img,cmap=plt.get_cmap('gray'))
plt.title("original")

plt.subplot(1,3,2)
plt.imshow(output_64,cmap=plt.get_cmap('gray'))
plt.title("reduced image")

plt.subplot(1,3,3)
plt.imshow(nn_interpolation,cmap=plt.get_cmap('gray'))
plt.title("nn_interpolation")

plt.tight_layout()
plt.show()
<Figure size 640x480 with 0 Axes>
In [ ]:
# bilinear_interpolation
bilinear_interpolation = helper.bilinear_interpolation(output_64,4)
In [ ]:
# plot the images
plt.figure()
plt.subplots(figsize=(15, 5))
plt.subplot(1,3,1)
plt.imshow(img,cmap=plt.get_cmap('gray'))
plt.title("original")

plt.subplot(1,3,2)
plt.imshow(output_64,cmap=plt.get_cmap('gray'))
plt.title("reduced image")

plt.subplot(1,3,3)
plt.imshow(bilinear_interpolation,cmap=plt.get_cmap('gray'))
plt.title("bilinear_interpolation")

plt.tight_layout()
plt.show()
<Figure size 640x480 with 0 Axes>
In [ ]:
#cubic interpolation
cubic_interpolation = helper.cubic_interpolation(output_64,4)
In [ ]:
# plot the images
plt.figure()
plt.subplots(figsize=(15, 5))
plt.subplot(1,3,1)
plt.imshow(img,cmap=plt.get_cmap('gray'))
plt.title("original")

plt.subplot(1,3,2)
plt.imshow(output_64,cmap=plt.get_cmap('gray'))
plt.title("reduced image")

plt.subplot(1,3,3)
plt.imshow(cubic_interpolation,cmap=plt.get_cmap('gray'))
plt.title("cubic_interpolation")

plt.tight_layout()
plt.show()
<Figure size 640x480 with 0 Axes>
In [ ]:
# Linear interpolation
linear_interpolation = helper.linear_interpolation(output_64,4)
In [ ]:
# plot the images
plt.figure()
plt.subplots(figsize=(15, 5))
plt.subplot(1,3,1)
plt.imshow(img,cmap=plt.get_cmap('gray'))
plt.title("original")

plt.subplot(1,3,2)
plt.imshow(output_64,cmap=plt.get_cmap('gray'))
plt.title("reduced image")

plt.subplot(1,3,3)
plt.imshow(linear_interpolation,cmap=plt.get_cmap('gray'))
plt.title("linear_interpolation")

plt.tight_layout()
plt.show()
<Figure size 640x480 with 0 Axes>
In [ ]:
# spline interpolation
spline_interpolation = helper.spline_interpolation(output_64,4)
In [ ]:
# plot the images
plt.figure()
plt.subplots(figsize=(15, 5))
plt.subplot(1,3,1)
plt.imshow(img,cmap=plt.get_cmap('gray'))
plt.title("original")

plt.subplot(1,3,2)
plt.imshow(output_64,cmap=plt.get_cmap('gray'))
plt.title("reduced image")

plt.subplot(1,3,3)
plt.imshow(spline_interpolation,cmap=plt.get_cmap('gray'))
plt.title("spline_interpolation")

plt.tight_layout()
plt.show()
<Figure size 640x480 with 0 Axes>
In [ ]:
img=img[:512,:512]
In [ ]:
img.shape
Out[ ]:
(512, 512)
In [ ]:
#psnr
print("PSNR for nn_interpolation is ",PSNR(img,nn_interpolation))
print("PSNR for bilinear_interpolation is ",PSNR(img,bilinear_interpolation))
print("PSNR for cubic_interpolation is ",PSNR(img,cubic_interpolation))
print("PSNR for linear_interpolation is ",PSNR(img,linear_interpolation))
print("PSNR for spline_interpolation is ",PSNR(img,spline_interpolation))
PSNR for nn_interpolation is  24.010932600998878
PSNR for bilinear_interpolation is  21.71993236161517
PSNR for cubic_interpolation is  33.16004251272271
PSNR for linear_interpolation is  32.988441393556734
PSNR for spline_interpolation is  33.15633465133061
In [ ]:
# Save the image
imsave("noise_image_5.bmp",noise_image_5)
imsave("removed_noise_5.bmp",removed_noise_5)
imsave("noise_image_15.bmp",noise_image_15)
imsave("removed_noise_15.bmp",removed_noise_15)
imsave("noise_image_20.bmp",noise_image_20)
imsave("removed_noise_20.bmp",removed_noise_20)
imsave("noise_image_25.bmp",noise_image_25)
imsave("removed_noise_25.bmp",removed_noise_257)

imsave("nn_interpolation.bmp",nn_interpolation)
imsave("bilinear_interpolation.bmp",bilinear_interpolation)
imsave("cubic_interpolation.bmp",cubic_interpolation)
imsave("linear_interpolation.bmp",linear_interpolation)
imsave("spline_interpolation.bmp",spline_interpolation)
Lossy conversion from float64 to uint8. Range [0.0, 255.0]. Convert image to uint8 prior to saving to suppress this warning.
Lossy conversion from float64 to uint8. Range [0.0, 255.0]. Convert image to uint8 prior to saving to suppress this warning.
Lossy conversion from float64 to uint8. Range [0.0, 255.0]. Convert image to uint8 prior to saving to suppress this warning.
Lossy conversion from float64 to uint8. Range [0.0, 255.0]. Convert image to uint8 prior to saving to suppress this warning.
Lossy conversion from float64 to uint8. Range [0.0, 255.0]. Convert image to uint8 prior to saving to suppress this warning.
Lossy conversion from float64 to uint8. Range [0.0, 255.0]. Convert image to uint8 prior to saving to suppress this warning.
Lossy conversion from float64 to uint8. Range [0.0, 255.0]. Convert image to uint8 prior to saving to suppress this warning.
Lossy conversion from float64 to uint8. Range [0.0, 255.0]. Convert image to uint8 prior to saving to suppress this warning.
Lossy conversion from float64 to uint8. Range [8.0, 250.0]. Convert image to uint8 prior to saving to suppress this warning.
Lossy conversion from float64 to uint8. Range [8.0, 250.0]. Convert image to uint8 prior to saving to suppress this warning.